function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function getAllQueryString() {
    const url = location.search;
    if (url.indexOf("?") != -1) {
        return url.substr(url.indexOf("?"))
    }
    return '';
}

var getMenuById = function () {
    var id = getQueryString('id')
    if (!id) {
        return;
    }
    $.post(baseUrl + 'admin/sysMenu/get', {id: id}, function (result) {
        var r = result.value;
        $("input[name='id']").val(r.id);
        $("input[name='name']").val(r.name);
        $("input[name='funUrl']").val(r.funUrl);
        $("input[name='sort']").val(r.sort);
        $("input[name='className']").val(r.className);

        if (r.type === "1") {
            $("#parentId-css").hide();
            $("#parentId").val(null);
        } else {
            selectMenu(r.type, r.parentId)
        }
        $('input:radio[name="type"][value=' + r.type + ']').iCheck('check');
        $("input[name='parentId']").val(r.parentId);
        $("input[name='parentValue']").val(r.parentId);
    })
}

var saveMenu = function () {
    var str = $("#myForm").serializeArray();
    $.ajax({
        url: baseUrl + 'admin/sysMenu/save',
        type: 'post',
        data: str,
        success: function (data) {
            if (data.code === 0) {
                goBack();
            } else {
                layer.msg(data.message);
            }
        },
        error: function () {
            layer.msg("请求出错")
        }
    });
}

var selectMenu = function (type, parentId) {
    $("#parentId-css").show();
    $(".chosen-select").chosen();
    $.post(baseUrl + 'admin/sysMenu/listAll', {type: type, parentId: parentId}, function (result) {
        if (result.value === null) {
            return;
        }
        var parentValue = $('#parentValue').val();
        var optHtml;
        result.value.forEach(function (t) {
            var isS = parentValue.indexOf(t.id) > -1 ? 'selected' : '';
            optHtml += '<option value="' + t.id + '" ' + isS + '>' + t.name + '</option>';
        })
        $(".chosen-select").append(optHtml);
        $(".chosen-select").trigger('chosen:updated');
    });
}

$(document).ready(function () {
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });

    //获取信息
    getMenuById();


    var mtype = getQueryString('type');
    if (mtype) {
        var chkType = parseInt(mtype) + 1 + '';
        $('input:radio[name="type"][value=' + chkType + ']').iCheck('check');
        $('#parentValue').val(getQueryString("parentId"));
        selectMenu(mtype);
    }


    $("#btnSave").click(function () {
        saveMenu();
    });

    $('input[name="type"]').on('ifChecked', function () {
        var value = $(this).val();
        if (value === "1") {
            $("#parentId-css").hide();
            $("#parentId").val(null);
        } else {
            selectMenu(value - 1);
        }
    })
});

function goBack() {
    location.href = baseUrl + 'admin/system/menu-list.html' + getAllQueryString();
}